package com.lantosec.hmrp.modules.supplier.controller.app;


import com.lantosec.hmrp.core.kit.PageVO;
import com.lantosec.hmrp.core.kit.Result;
import com.lantosec.hmrp.core.mvc.BaseController;
import com.lantosec.hmrp.core.util.DateUtils;
import com.lantosec.hmrp.core.util.UUIDUtils;
import com.lantosec.hmrp.modules.supplier.entity.SupplierBad;
import com.lantosec.hmrp.modules.supplier.service.ISupplierBadService;
import com.lantosec.hmrp.modules.supplier.service.ISupplierInfoService;
import com.lantosec.hmrp.modules.supplier.vo.SupplierBadVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 *
 * 供应商不良记录 控制器
 *
 * @author AutoCode
 * @since 2020-02-21
 */
@Api(value = "供应商不良记录控制器APP端", tags = "供应商不良记录控制器APP端")
@RestController
@RequestMapping("/supplierBadApp")
@Log4j2
public class SupplierBadAppController extends BaseController {
    @Autowired
    private ISupplierBadService service;

    @Autowired
    private ISupplierInfoService iSupplierInfoService;

    @GetMapping("all")
    @ApiOperation(value = "查询全部")
    public Result<List<SupplierBadVO>> all(){
        return Result.ok(entity2VO(service.list()));
    }

    @GetMapping("querySupplierBad")
    @ApiOperation(value = "分页查询")
    public Result<PageVO<SupplierBadVO>> querySupplierBad(@ApiParam(value = "分页对象", required = true) PageVO<SupplierBadVO> page,
                                                          @ApiParam(value = "供应商") @RequestParam(name = "supplierCode",required = false) String supplierCode,
                                                          @ApiParam(value = "不良类型") @RequestParam(name = "badType",required = false) String badType,
                                                          @ApiParam(value = "开始时间") @RequestParam(name = "startTime",required = false) String startTime,
                                                          @ApiParam(value = "结束时间") @RequestParam(name = "endTime",required = false) String endTime){
        PageVO<SupplierBadVO> pageVO = service.querySupplierBad(page,supplierCode,badType,startTime,endTime);
        return Result.ok(pageVO);
    }

    @GetMapping("querySupplierBadById/{badId}")
    @ApiOperation(value = "主键查询")
    public Result<SupplierBadVO> get(@ApiParam(value = "ID", required = true) @PathVariable(name="badId") String badId){
        return Result.ok(entity2VO(service.getById(badId)));
    }

    @PostMapping("insertSupplierBad")
    @ApiOperation(value = "新增数据")
    public Result<String> insertSupplierBad(@ApiParam(value = "供应商不良记录VO对象", required = true) @RequestBody @Validated SupplierBadVO vo, BindingResult bindingResult){
        validationHandler(bindingResult);
        SupplierBad entity = vo.convertToEntity();
        entity.setBadId(UUIDUtils.get());
        //通过供应商编码查询相对应的供应商名称
        String supplierName = iSupplierInfoService.getById(entity.getSupplierCode()).getSupplierName();
        entity.setSupplierName(supplierName);
        entity.setStopFlag(0);
        entity.setBlacklistFlag(0);
        entity.setSubmitFlag(0);
        entity.setCreateUserId("111");
        entity.setCreateUser("张三");
        entity.setCreateTime(DateUtils.nowDate());
        service.save(entity);
        return Result.ok(entity.getBadId());
    }

    @PutMapping("updateSupplierBad")
    @ApiOperation(value = "编辑不良记录")
    public Result<Void> updateSupplierBad(@ApiParam(value = "供应商不良记录VO对象", required = true) @RequestBody SupplierBadVO vo){
        SupplierBad supplierBad = vo.convertToEntity();
        service.updateById(supplierBad);
        return Result.ok();
    }

    @PutMapping("managementSupplierBad")
    @ApiOperation(value = "处置不良记录")
    public Result<Void> managementSupplierBad(@ApiParam(value = "供应商不良记录VO对象", required = true) @RequestBody List<SupplierBadVO> voList){
        service.managementSupplierBad(voList);
        return Result.ok();
    }

    @DeleteMapping(value = "delete/{ids}")
    @ApiOperation("逻辑删除")
    public Result<Void> delete(@ApiParam(value = "ID列表,用逗号分隔", required = true) @PathVariable(name="ids") String ids){
        if(StringUtils.isNotEmpty(ids)){
            service.removeByIds(Arrays.asList(ids.split(",")));
        }
        return Result.ok();
    }
}
